package com.nahan.secondapplication.AppUpdate

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.nahan.secondapplication.MyUtils
import com.nahan.secondapplication.R

/**
 * 圆角弹出框
 */
class AppUpdateDialog(context: Context?, alert: Boolean) : Dialog(context!!, R.style.utils_circle_dialog), View.OnClickListener {
    var mOnRightListener: OnRightListener? = null
    var mOnLeftListener: OnLeftListener? = null
//    val tv_left: TextView
//    val tv_right: TextView
    val tv_title: TextView
    val tv_msg: TextView



    init {
        val view = LayoutInflater.from(context).inflate(R.layout.app_update_dialog, null)
//        tv_left = view.findViewById<TextView>(R.id.tv_left)
//        tv_right = view.findViewById<TextView>(R.id.tv_right)
        tv_title = view.findViewById<TextView>(R.id.tv_title)
        tv_msg = view.findViewById<TextView>(R.id.tv_msg)

        //绑定两个按钮的事件
//        tv_left.setOnClickListener(this)
//        tv_right.setOnClickListener(this)

        setContentView(view)
        val window = window
        val params = window!!.attributes
        params.gravity = Gravity.CENTER
        val d = window.windowManager.defaultDisplay
        params.width = (d.width * 0.85).toInt()
        window.attributes = params
        window.setWindowAnimations(R.style.utils_circle_dialog_Animation)//缩放动画


    }

    companion object {

        var yangDialog: AppUpdateDialog? = null

        @JvmOverloads
        fun showDialog(mActivity: Activity?, tv_title: String?, tv_msg: String?, leftText: String?, rightText: String?, leftListener: OnLeftListener?, rightListener: OnRightListener?, gravity: Int = Gravity.CENTER, cancelable: Boolean = false) {
            if (MyUtils.activityIsFinished(mActivity)) {
                return  //activity被销毁，不显示
            }
            yangDialog = AppUpdateDialog(mActivity, false)
            //不能点外面取消
            yangDialog!!.setCancelable(cancelable)
            yangDialog!!.setCanceledOnTouchOutside(cancelable)
            yangDialog!!.setContext(tv_title,tv_msg,leftText, rightText, gravity)
            yangDialog!!.setRightListener(rightListener)
            yangDialog!!.setLeftListener(leftListener)
            yangDialog!!.setOwnerActivity(mActivity!!)
            yangDialog!!.setOnDismissListener { yangDialog = null }
            yangDialog!!.show()

        }

        fun dismissDialog(mActivity: Activity) {
            if (yangDialog != null && yangDialog!!.isShowing && yangDialog!!.ownerActivity != null && yangDialog!!.ownerActivity.toString() == mActivity.toString()) {
                yangDialog!!.dismiss()
                yangDialog = null
            }
        }


    }


    fun setContext(tv_title: String?, tv_msg: String?,leftText: String?, rightText: String?, gravity: Int) {
//        if (leftText == null) {
//            tv_left.visibility = View.GONE
//        } else {
//            tv_left.visibility = View.VISIBLE
//            tv_left.text = leftText
//        }
//        if (rightText == null) {
//            tv_right.visibility = View.GONE
//        } else {
//            tv_right.visibility = View.VISIBLE
//            tv_right.text = rightText
//        }





    }

    override fun onClick(v: View) {
//        if (v.id == R.id.tv_right && mOnRightListener != null) { //确认
//            if (TextUtils.isEmpty(tv_time.text.toString())) {
//                ToastUtils.showShort("请先选择开课时间")
//                return
//            }
//            mOnRightListener?.onClick(this)
//        } else if (v.id == R.id.tv_left && mOnLeftListener != null) { //取消
//            mOnLeftListener?.onClick(this)
//        }
        dismiss()
    }

    fun setRightListener(l: OnRightListener?) {
        mOnRightListener = l
    }

    fun setLeftListener(l: OnLeftListener?) {
        mOnLeftListener = l
    }

    interface OnRightListener {
        fun onClick(yangDialog: AppUpdateDialog?)
    }

    interface OnLeftListener {
        fun onClick(yangDialog: AppUpdateDialog?)
    }






}